/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

Ext.ns("CRM.SPComp");

(function() {

	CRM.SPComp.createNewHeader = function(firstActiveTabIndex, secondActiveTabIndex) {

		function redirectPage(absoluteUrl){
			var newPath = absoluteUrl;
			var currentUrl = window.location.href;
			var currentPath = currentUrl.substring(currentUrl.lastIndexOf("/")+1, currentUrl.length);
			if(currentPath != newPath){
				window.location = newPath;
				}
		};
		
		function redirectPageToSales(absoluteUrl, salesSubTabActiveTabIndex){
			var newPath = absoluteUrl;
			var currentUrl = window.location.href;
			var currentPath = currentUrl.substring(currentUrl.lastIndexOf("/")+1, currentUrl.length);
			if(currentPath != newPath){
				window.location = newPath;
				}
				salesTab.setActiveTab(salesSubTabActiveTabIndex);
				
				
		};	
		
		var consoleTab = {
			xtype 	: 'tabpanel',
			id 		: 'consoleTab',
			title 	: 'Console',
			items 	: []
			
		};

		var calendarTab = {
			xtype 	: 'tabpanel',
			id 		: 'calendarTab',
			title 	: 'Calendar',
			items 	: [],
			listeners:{activate :function(){
			addComponent(createCalender());
		}
			
		}
		};
		
		var leadTab = {
			xtype 	: 'tabpanel',
			id 		: 'leadTab',
			title 	: 'Lead',
			region	: 'center',
			items 	: [],
			listeners : {activate : function(){
							addComponent(createLeadPanel());
							applyLeadAuthorization();
							Ext.getCmp('editButton').disable();	
							Ext.getCmp('topEditButton').disable();
						}}
		};
		
		var customerTab = {
			xtype 	: 'tabpanel',
			id 		: 'customerTab',
			title 	: 'Customer',
			items 	: [],
			listeners: {activate : function(){
							addComponent(createCustomerPanel());
							applyCustomerAuthorization();
							Ext.getCmp('editButton').disable();
							Ext.getCmp('topEditButton').disable();
						}}
		};
		
		var opportunityTab = {
			xtype 	: 'tabpanel',
			id 		: 'opportunityTab',
			title 	: 'Opportunity',
			items 	: [],
			listeners: {activate : function(){
							addComponent(createOpportunityPanel());
							applyOpportunityAuthorization();
						}}
		};
		
		var activityTab = {
			xtype 	: 'tabpanel',
			id 		: 'activityTab',
			title 	: 'Activity',
			items 	: [],
			listeners: {activate : function(){
							 addComponent(createActivityPanel());
							 applyActivityAuthorization();
						}}
		};
		
		var searchTab = {
			xtype 	: 'tabpanel',
			id 		: 'searchTab',
			title 	: 'Search',
			items 	: [],
			listeners : { activate: 
				function(){
						addComponent(createLeadChartPanel());
						
						//redirectPageToSales('leadchart', secondActiveTabIndex);
		}		}
		};
		
		var reportTab = {
			xtype 	: 'panel',
			id 	  	: 'reportTab',
			title 	: 'Report',
			items 	: [],
			listeners: {activate : function(){
							addComponent(createReportPanel());
						}}
			};
		
		
		//end of sales tab sub tabs
		
		
		
		//project modules tab
		var projectHomeTab = {
			xtype 	: 'panel',
			id    	: 'projectHomeTab',
			title 	: 'Home',
			items	: []
		}; 
		
		var projectInfoTab = {
			xtype 	: 'panel',
			id    	: 'projectInfoTab',
			title 	: 'Project Manager',
			items	: [],
			listeners: {activate : function(){
						   	addComponent(createProjectModulePanel());
						}}
		};
		
		var projectTaskTab = {
			xtype 	: 'panel',
			frame:false,
			id    	: 'projectTaskTab',
			width:550,
			title 	: 'Task',
			items	: [],
			listeners: {activate : function(){
			
			   	addComponent(createTaskModule());
			}}
		};
		
		var projectMemberTab = {
			xtype 	: 'panel',
			id    	: 'projectMemberTab',
			title 	: 'Member',
			items	: []
		};
		
		var projectIssueTab = {
			xtype    : 'panel',
			id       : 'projectIssueTab', 
			title    : 'Issues',
			items    : [],
			listeners: {activate : function(){
					addComponent(createIssueModule());
			}}

		 };
		
		var projectEngagementTab = {
			xtype 	  : 'panel',
			id    	  : 'projectEngagementTab',
			title 	  : 'Engagement',
			items	  : []
		};		
		
		//admin module tabs
		var userManagerPanel = {
				xtype	  	: 'panel',
				id		  	: 'userManagerTab',
				title	  	: 'User Manager',
				items	  	: [],
				listeners	: {activate: function(){
									addComponent(createUserManagerPanel());
							  }}
		};
		
		
		var permissionPanel = {
				xtype	  	: 'panel',
				id		  	: 'permissionPanel',
				title	  	: 'Permissions',
				items	  	: [],
				listeners 	: {activate: function(){
									addComponent(createPermissionMainPanel());
							  }}
		};
		
		var setupTab = {
				xtype 	  	: 'panel',
				id 		  	: 'setupsid',
				title 	  	: 'Setups',
				items 		: [],
				listeners	: {activate : function(){
									addComponent(createSetupPanel());
							  }}
		};
		
		
		//master panels
		var salesTab = new Ext.TabPanel({
			xtype 	  : 'tabpanel',
			title 	  : 'Sales',
			id 		  : 'salesTab',
			activeTab : 0,
			anchor		: '100%',
			items 	  : [ consoleTab, 
				          calendarTab,
				          leadTab, 
				          customerTab,
				          opportunityTab, 
				          activityTab, 
				          searchTab, 
				          reportTab
				        ],
			listeners : {activate: function(){
								removeComponent('salesTab');
						}}	        
		});

		var marketingTab = new Ext.TabPanel({
			xtype 		: 'tabpanel',
			title 		: 'Marketing',
			id 	  		: 'marketingTab',
			items 		: [],
			listeners 	: {activate: function(){
								removeComponent('marketingTab');
						  }}
		});		
		
		var projectTab = new Ext.TabPanel({
			xtype		: 'tabpanel',
			title		: 'Project',
			id			: 'projectTab',
			activeTab	: 0,
			items		: [projectHomeTab, projectInfoTab, projectTaskTab, /*projectMemberTab,*/ projectIssueTab, projectEngagementTab],
			listeners 	: {activate: function(){
								removeComponent('projectTab');
						}}
		});
		
		var adminTab = new Ext.TabPanel({
			xtype		: 'tabpanel',
			title		: 'Administrator',
			id			: 'adminTab',
			activeTab	: 0,
			items		: [userManagerPanel, permissionPanel, setupTab],
			listeners 	: {activate: function(){
								removeComponent('adminTab');
								addComponent(createUserManagerPanel());
						}}
		});		

		var headerTab = new Ext.TabPanel({
			anchor		: '100%',
			height 		: 52,
			id 			: 'headerTab',
			activeTab 	: 0,
			items 		: [salesTab, marketingTab, projectTab, adminTab]
		});
		
		var tbarPanel = {
			xtype		: 'panel',
			id			: 'tbarPanel',
			layout		: 'vbox',
			buttons		: []
		};
		
		var mainCmpPanel = new Ext.Panel({
			id			: 'mainCmpPanel',
			anchor		: '100%',
			height		: 800,
			scrollable	: true,
			frame		: false
		});
		
		var mainHeaderPanel = new Ext.Panel({
			anchor		: '100%',
			autoHeight	: true,
			id 			: 'mainPanel',
			frame		: true,
			scrollable	: true,
			items 		: [
			      		   	headerTab, mainCmpPanel
			      		   ]
			/*
			,
            tbar		: {
                		   		xtype	: 'toolbar',
                		   		anchor	: '100%',
                		   		buttonAlign	: 'right',
                		   		items	: [
                		   		     	   	{
											  	xtype	: 'displayfield',
											  	id		: 'loginDetailsTF',
											  	autoWidth: true,
											  	value	: 'SourcePundit InfoSystem (P) LTD.'
											 },			       
											 '-',
							    			 {
											    xtype	: 'button',
											    text	: 'Logout',
											    width	: 50,
											    handler	: logout
											 }
                		   		     	   ]
            				}
			*/
		});
		
		login();
		return mainHeaderPanel;
	};
	
	function removeComponent(tabName){
		var cmpPanel = Ext.getCmp('mainCmpPanel'); 
		cmpPanel.removeAll();
		Ext.getCmp(tabName).setActiveTab(0);
		cmpPanel.doLayout();
	};
	
	function addComponent(component){
		var cmpPanel = Ext.getCmp('mainCmpPanel');
		cmpPanel.removeAll();
		cmpPanel.add(component);
		cmpPanel.doLayout();
		component.doLayout();
	};
	
	
	function login(){
		Ext.Ajax.request({
			url			: 'home/loginDetails',
			method		: 'GET',
			waitMessage : 'Loading...',
			success		: function(result, request){
							var jsonData = Ext.util.JSON.decode(result.responseText);
							//Ext.getCmp('loginDetailsTF').setValue(jsonData.data);
							getAuthorizationDetails();
						 },
			failure		: function(){
							alert("User Details not found.");
						 }
		});
	};
	
	function logout(){
		Ext.Ajax.request({
			url		: 'login/logout',
			method	: 'GET',
			success	: function(){
						 Ext.Msg.alert('Confirm', 'Successfully Logout');
						 window.location = 'login';
						},
			failure	: function(){
							Ext.Msg.alert('Error', 'Failure');
						}			
		});
	}
	
	var vCreate = null;
	var vRead = null;
	var vUpdate = null;
	var vRemove = null;
	var authData = null; 
	function getAuthorizationDetails(){
   	   Ext.Ajax.request({
   		   url		: 'home/getLoginDetails',
   		   method	: 'GET',
   		   success	: function(result, request){
  			  			var jsonData = Ext.util.JSON.decode(result.responseText); 
   			  			authData = jsonData.data;
   			   		  },
   		   failure 	: function(){
   			   			alert("failure");
   			   		  }   		  
   	   });
	};	
	
	function showAuthenticationMessage(message){
		Ext.MessageBox.show({
	            title	: 'Alert',
	            msg	 	: message,
	            width	: 200,
	            buttons	: Ext.MessageBox.OK
	           });
	};
	
	function applyLeadAuthorization(){
		vCreate = authData.resourceDetails.LEAD.create; 
		vRead = authData.resourceDetails.LEAD.read;
		vUpdate = authData.resourceDetails.LEAD.update;
		vRemove = authData.resourceDetails.LEAD.remove;
  		//alert(vCreate+", "+vRead+", "+vUpdate+", "+vRemove);

		if(vCreate == false){
			Ext.getCmp('saveButton').disable();
			Ext.getCmp('topSaveButton').disable();
				
			//Ext.getCmp('newButton').disable();
			//Ext.getCmp('topNewButton').disable();
		}
		if(vRead == false){
			showAuthenticationMessage('Sorry! You don\'t have access to this page.');
			removeComponent('salesTab');
		}		
		if(vUpdate == false){
			Ext.getCmp('editButton').disable();
			Ext.getCmp('topEditButton').disable();
		}
		if(vRemove == false){
			//Ext.getCmp('removeButton').disable();	
			//Ext.getCmp('topRemoveButton').disable();
		}		
	};
	
	function applyCustomerAuthorization(){
		vCreate = authData.resourceDetails.CUSTOMER.create; 
		vRead = authData.resourceDetails.CUSTOMER.read;
		vUpdate = authData.resourceDetails.CUSTOMER.update;
		vRemove = authData.resourceDetails.CUSTOMER.remove;
  		//alert(vCreate+", "+vRead+", "+vUpdate+", "+vRemove);
		if(vCreate == false){
			Ext.getCmp('saveButton').disable();
			Ext.getCmp('topSaveButton').disable();
				
			//Ext.getCmp('newButton').disable();
			//Ext.getCmp('topNewButton').disable();
		}
		if(vRead == false){
			showAuthenticationMessage('Sorry! You don\'t have access to this page.');
			removeComponent('salesTab');
		}
		if(vUpdate == false){
			Ext.getCmp('editButton').disable();
			Ext.getCmp('topEditButton').disable();
		}
		if(vRemove == false){
			//Ext.getCmp('removeButton').disable();	
			//Ext.getCmp('topRemoveButton').disable();
		}
	};
	
	function applyActivityAuthorization(){
      	//alert(vCreate+", "+vRead+", "+vUpdate+", "+vRemove);
	   	if(vCreate == false){
	   		Ext.getCmp('saveButton').disable();
	   		Ext.getCmp('topSaveButton').disable();
	   			
	   		//Ext.getCmp('newButton').disable();
	   		//Ext.getCmp('topNewButton').disable();
	   	}
		if(vRead == false){
			showAuthenticationMessage('Sorry! You don\'t have access to this page.');
			removeComponent('salesTab');
		}
	   	if(vUpdate == false){
	   		Ext.getCmp('editButton').disable();
	   		Ext.getCmp('topEditButton').disable();
	   	}
      	if(vRemove == false){
	   		//Ext.getCmp('removeButton').disable();	
	   		//Ext.getCmp('topRemoveButton').disable();
	   	}
      };
      
      function applyOpportunityAuthorization(){
        	//alert(vCreate+", "+vRead+", "+vUpdate+", "+vRemove);
  	   	if(vCreate == false){
  	   		Ext.getCmp('saveButton').disable();
  	   		Ext.getCmp('topSaveButton').disable();
  	   				
  	   		//Ext.getCmp('newButton').disable();
  	   		//Ext.getCmp('topNewButton').disable();
  	   	}
		if(vRead == false){
			showAuthenticationMessage('Sorry! You don\'t have access to this page.');
			removeComponent('salesTab');
		}
  	   	if(vUpdate == false){
  	   		Ext.getCmp('editButton').disable();
  	   		Ext.getCmp('topEditButton').disable();
  	   	}
        if(vRemove == false){
  	   		//Ext.getCmp('removeButton').disable();	
  	   		//Ext.getCmp('topRemoveButton').disable();
  	   	}
      };

})();